package com.app.store.dao.impl;

import com.app.store.dao.StoreDao;
import com.app.store.model.AppVersion;
import com.app.sysmodule.dao.impl.BaseDaoImpl;
import com.app.sysmodule.exception.DBException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;

import java.util.List;

/**
 * Created by song on 2017/12/20.
 */
public class StoreDaoImpl extends BaseDaoImpl<AppVersion> implements StoreDao {
//    public AppVersion getVersion(String version) throws DBException {
//        try{
//            String hql = " from AppVersion where isDelete=0 and serverVersion<=(:version) ORDER BY createDate DESC ";
//            return (AppVersion)this.getSession().createQuery(hql).setParameter("version",version).setMaxResults(1).uniqueResult();
//        }catch (Exception e){
//            throw new DBException(e.getMessage());
//        }
//    }

    public AppVersion getVersion1(String version, String type,String source) throws DBException {
        AppVersion ver = new AppVersion();
        try{
            String sql = "SELECT * from a_version where is_delete=0 and INET_ATON(server_version)<=INET_ATON(:version) and type=(:type) and source=(:source) ORDER BY INET_ATON(app_version) DESC";
            SQLQuery sqlQuery = this.getSession().createSQLQuery(sql);
            sqlQuery.addEntity(AppVersion.class).setParameter("version",version).setParameter("type",type).setParameter("source",source).setMaxResults(1);
            List<AppVersion> result=sqlQuery.list();
            if(null != result && result.size()>0){
                ver  =  (AppVersion)result.get(0);
            }
        }catch (Exception e){
            throw new DBException(e.getMessage());
        }
        return ver;
    }

    /**
     * 最后一次强制更新版本
     * @param version
     * @param type
     * @param source
     * @return
     * @throws DBException
     */
    public AppVersion getVersion2(String version, String type,String source) throws DBException {
        AppVersion ver = new AppVersion();
        try{
            String sql = "SELECT * from a_version where is_delete=0 AND is_upgrade=1 and INET_ATON(server_version)<=INET_ATON(:version) and type=(:type) and source=(:source) ORDER BY INET_ATON(app_version) DESC";
            SQLQuery sqlQuery = this.getSession().createSQLQuery(sql);
            sqlQuery.addEntity(AppVersion.class).setParameter("version",version).setParameter("type",type).setParameter("source",source).setMaxResults(1);
            List<AppVersion> result=sqlQuery.list();
            if(null != result && result.size()>0){
                ver  =  (AppVersion)result.get(0);
            }
        }catch (Exception e){
            throw new DBException(e.getMessage());
        }
        return ver;
    }

    public AppVersion findVersionByTypeAndVersion(String type, String version) throws DBException{
        try{
            String hql = " from AppVersion where isDelete = 0 and type=(:type) and appVersion=(:version)";
            Query query = this.getSession().createQuery(hql).setParameter("type",type).setParameter("version",version);
            List<AppVersion> list = query.list();
            if(null != list && list.size()>0){
                return (AppVersion)list.get(0);
            }
        }catch (Exception e){
            throw new DBException(e.getMessage());
        }
        return null;
    }

    @Override
    public AppVersion getVersionByType(String type, String source) throws DBException {
        try{
            String sql = " SELECT * from a_version where is_delete =0 and type=(:type) and source=(:source) ORDER BY INET_ATON(app_version) DESC ";
            SQLQuery sqlQuery = this.getSession().createSQLQuery(sql);
            sqlQuery.addEntity(AppVersion.class).setParameter("type",type).setParameter("source",source).setMaxResults(1);
            List<AppVersion> result=sqlQuery.list();
            if(null != result && result.size()>0){
                return (AppVersion)result.get(0);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new DBException(e.getMessage());
        }
        return null;
    }

    @Override
    public List<AppVersion> getDeleteInfo(String serverVersion, String type, Integer size) throws DBException {
        try {
            String sql = "SELECT * FROM a_version WHERE type=? and server_version = ? AND id NOT IN ( SELECT t.id FROM ( SELECT a.id FROM a_version AS a where a.type=? and a.server_version = ? ORDER BY INET_ATON(app_version) DESC LIMIT ? ) AS t );";
            return this.getSession().createSQLQuery(sql).addEntity(AppVersion.class).setParameter(0, type).setParameter(1, serverVersion).setParameter(2, type).setParameter(3, serverVersion).setParameter(4, size).list();
        } catch (Exception e) {
            e.printStackTrace();
            throw new DBException(e.getMessage());
        }

    }

    @Override
    public void batchDelete(List<Long> idList) throws DBException {
        try{
            String sql = "delete from a_version where id in (:idList)";
            this.getSession().createSQLQuery(sql).setParameterList("idList",idList).executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
            throw new DBException(e.getMessage());
        }
    }

    @Override
    public List<String> getServerVersionList(String source) throws DBException {
      try{
            String sql = "SELECT server_version from  a_version where is_delete=0 and source=(:source) GROUP BY server_version ORDER BY INET_ATON(server_version) DESC";
            return (List<String>) this.getSession().createSQLQuery(sql).setParameter("source",source).list();
      }catch (Exception e){
         throw new DBException(e.getMessage());
      }
    }
}
